﻿namespace JsViewEngine
{
    using System.Web.Mvc;

    /// <summary>
    /// Represents an implementation of the <see cref="T:System.Web.Mvc.IViewEngine"/> interface.
    /// </summary>
    public class JsTemplateViewEngine : VirtualPathProviderViewEngine
    {
        public string ScriptPathJsrenderJs { get; set; }
        public string ScriptPathViewConvertersJs { get; set; }

        public JsTemplateViewEngine()
        {
            this.PartialViewLocationFormats = new[] { "~/Views/{1}/{0}.jsrender", "~/Views/Shared/{0}.jsrender" };

            // Full views is not really supported, but is required anyways.
            this.ViewLocationFormats = this.PartialViewLocationFormats;
            this.ScriptPathJsrenderJs = "~/Scripts/jsrender.js";
            this.ScriptPathViewConvertersJs = "~/Scripts/viewconverters.js";
        }

        /// <summary>
        /// Creates the specified partial view by using the specified controller context.
        /// </summary>
        /// <returns>
        /// A reference to the partial view.
        /// </returns>
        /// <param name="controllerContext">The controller context.</param><param name="partialPath">The partial path for the new partial view.</param>
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var physicalPath = controllerContext.HttpContext.Server.MapPath(partialPath);
            var jsrenderPath = controllerContext.HttpContext.Server.MapPath(this.ScriptPathJsrenderJs);
            var viewconvertersPath = controllerContext.HttpContext.Server.MapPath(this.ScriptPathViewConvertersJs);

            return new JsTemplateView(physicalPath, jsrenderPath, viewconvertersPath);
        }

        /// <summary>
        /// Creates the specified view by using the controller context, path of the view, and path of the master view.
        /// </summary>
        /// <returns>
        /// A reference to the view.
        /// </returns>
        /// <param name="controllerContext">The controller context.</param><param name="viewPath">The path of the view.</param><param name="masterPath">The path of the master view.</param>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            throw new System.NotImplementedException();
        }
    }
}